
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       blog.h
  * @author     baiyang
  * @date       2021-8-6
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <stdint.h>
#include <stdbool.h>

#include <common/gp_defines.h>
#include <parameter/param.h>
/*-----------------------------------macro------------------------------------*/
// buffer长度和Sector大小定义
#define BLOG_BUFFER_SIZE         (32 * 1024U)
#define BLOG_SECTOR_SIZE         (1024U * 4) /* larger block can increase wrte bandwidth */
#define BLOG_MAX_SECTOR_TO_WRITE (BLOG_BUFFER_SIZE/BLOG_SECTOR_SIZE)
/*----------------------------------typedef-----------------------------------*/
enum {
    BLOG_STATUS_IDLE = 0,
    BLOG_STATUS_WRITE_HEAD,
    BLOG_STATUS_LOGGING,
    BLOG_STATUS_STOPPING,
};

typedef struct {
    volatile uint32_t head; // head point for sector
    volatile uint32_t tail; // tail point for sector
    uint32_t num_sector;
    uint32_t index; // index in sector
    uint8_t* data;
} blog_buffer_t;
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
gp_err blog_start(char* file_name);
void blog_stop(void);
void blog_flush(void);
bool blog_write_critical_block(const void* payload, uint16_t len);
bool blog_write_file(const void* payload, uint16_t len);
uint8_t blog_get_status(void);
char* blog_get_logging_file_name(void);

void blog_init(void);
int32_t blog_get_log_mode();
void blog_async_output(void);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



